/*这是通讯录的js*/
var functions_tongxunlu = {
		ajaxOfLoadOrg:function(id,parentid,clickedOrg,fromWhere){
			//先在隐藏域里记录通讯录是从哪里被激活的
			if(fromWhere!=undefined){
				$("#fromWhereOfActTongxunlu").val(fromWhere);
			}
			$.ajax({
				type : "POST",
				dataType : "JSON",
				async:false,
				url : basePath + '/tongxunlu/getDepartmentAndPerson.do',
				data :{"id":id,"parentid":parentid},
				success : function(result) {
					if(true==result.success){  
		            //如果id为undefined，parentid也为undefined就是加载顶级部门
					//如果id不为undefined，parentid为undefined就是加载该部门的子部门以及人员信息
					//如果id为undefined，parentid不为undefined就是加载该部门的父部门 以及 父部门的子部门、父部门的人员信息
					var list = result.data[0].jsonArray_department==null?null:result.data[0].jsonArray_department;
					var list_person = result.data[0].jsonArray_person==null?null:result.data[0].jsonArray_person;
					var list_parentDepartment = result.data[0].jsonObject_parentDepartment==null?null:result.data[0].jsonObject_parentDepartment;
					if(id==undefined && parentid==undefined && clickedOrg==undefined){
						//展示子部门
						if(list!==null){
							$(".parentOrg").remove();
						    $(".person").remove();
						    $(".org").remove();
						    //$(".checkedPerson").remove();
						    $(".noResultTips").hide();
							for(var i=0;i<list.length;i++){
								var span = $('<span>',{'class':'glyphicon glyphicon-chevron-right' ,'style':'float:right'});
								var name = list[i].name;
								if(name.length>15){
								   name=name.substr(0, 15)+"..."
								}
								var a = $('<a>').text(name);
								var li = $('<li>',{'class':'org','id':list[i].id,'parentid':list[i].parentid});
								a.append(span);
								li.append(a);
								$(".orgList").append(li);
						        }
							bindClick_tongxunlu.clickEveryOrg();
						}
						
					   }else if(id!=undefined && parentid==undefined){
						    $(".parentOrg").remove();
						    $(".person").remove();
						    $(".org").remove();
						    $(".noResultTips").hide();
						    clickedOrg.find("span").remove();
						    var span_left=$("<span>",{'class':'glyphicon glyphicon-chevron-left','style':'float: left'})
						    clickedOrg.find("a").append(span_left);
						    clickedOrg.removeClass("org");
						    clickedOrg.addClass("parentOrg");
						    $(".orgList").append(clickedOrg);
						    bindClick_tongxunlu.clickParentOrg();
						  //展示子部门
						    if(list!=null){
						    	for(var i=0;i<list.length;i++){
									var span = $('<span>',{'class':'glyphicon glyphicon-chevron-right' ,'style':'float:right'});
									var name = list[i].name;
									if(name.length>16){
									   name=name.substr(0, 15)+"..."
									}
									var a = $('<a>').text(name);
									var li = $('<li>',{'class':'org','id':list[i].id,'parentid':list[i].parentid});
									a.append(span);
									li.append(a);
									$(".orgList").append(li);
							      }
							    bindClick_tongxunlu.clickEveryOrg();
						    }
						  //展示子部门人员
							if(list_person!=null){
								for(var i=0;i<list_person.length;i++){
									var photourl = list_person[i].photourl==""?basePath+'/res/images/noPhotoUrl.jpg':list_person[i].photourl;
									var img = $('<img>',{'src':photourl,'style':'max-width: 40px;max-height: 40px;border-radius:40px'});
									var div_col2 = $('<div>',{'class':'col-xs-2'});
									div_col2.append(img);
									
									var span_name = $('<span>',{'style':'display: block;'}).text(list_person[i].name);
									var job = list_person[i].jobtitle;
									if(job.length>16){
										job=job.substr(0, 12)+"..."
									}
									var span_job = $('<span>',{'style':'font-size:12px;color:#969696;'}).text(job);
									var div_col8 = $('<div>',{'class':'col-xs-8'});
									div_col8.append(span_name);
									div_col8.append(span_job);
									
									var a = $('<a>',{'style':'height:60px'});
									a.append(div_col2);
									a.append(div_col8);
									
									var li = $('<li>',{'class':'row person','photourl':photourl,'openid':list_person[i].oid,'name':list_person[i].name});
									li.append(a);
									$(".orgList").append(li);
							        }
									//绑定人员点击事件
						    	    bindClick_tongxunlu.clickPerson();
							}
					   }else if(id==undefined && parentid!=undefined){
						    $(".person").remove();
					        $(".org").remove();
					        $(".parentOrg").remove();
					        $(".noResultTips").hide();
						   if(list_parentDepartment!=null){
							  if(list_parentDepartment.longnumber!="0"){
								  var span = $('<span>',{'class':'glyphicon glyphicon-chevron-left' ,'style':'float:left'});
								  var name = list_parentDepartment.name;
							      if(name.length>15){
									  name=name.substr(0, 15)+"..."
									}
								  var a = $('<a>').text(name);
								  var li = $('<li>',{'class':'parentOrg','id':list_parentDepartment.id,'parentid':list_parentDepartment.parentid});
								  a.append(span);
								  li.append(a);
								  $(".orgList").append(li);
								  bindClick_tongxunlu.clickParentOrg();
							  } 
						   }     
						 //展示父部门的子部门
						    if(list!=null){
						    	for(var i=0;i<list.length;i++){
									var span = $('<span>',{'class':'glyphicon glyphicon-chevron-right' ,'style':'float:right'});
									var name = list[i].name;
									if(name.length>15){
									   name=name.substr(0, 15)+"..."
									}
									var a = $('<a>').text(name);
									var li = $('<li>',{'class':'org','id':list[i].id,'parentid':list[i].parentid});
									a.append(span);
									li.append(a);
									$(".orgList").append(li);
							      }
							    bindClick_tongxunlu.clickEveryOrg();
						    }
						  //展示父部门的部门人员
							if(list_person!=null){
								for(var i=0;i<list_person.length;i++){
									var photourl = list_person[i].photourl==""?basePath+'/res/images/noPhotoUrl.jpg':list_person[i].photourl;
									var img = $('<img>',{'src':photourl,'style':'max-width: 40px;max-height: 40px;border-radius:40px'});
									var div_col2 = $('<div>',{'class':'col-xs-2'});
									div_col2.append(img);
									
									var span_name = $('<span>',{'style':'display: block;'}).text(list_person[i].name);
									var job = list_person[i].jobtitle;
									if(job.length>16){
										job=job.substr(0, 12)+"..."
									}
									var span_job = $('<span>',{'style':'font-size:12px;color:#969696;'}).text(job);
									var div_col8 = $('<div>',{'class':'col-xs-8'});
									div_col8.append(span_name);
									div_col8.append(span_job);
									
									var a = $('<a>',{'style':'height:60px'});
									a.append(div_col2);
									a.append(div_col8);
									
									var li = $('<li>',{'class':'row person','photourl':photourl,'openid':list_person[i].oid,'name':list_person[i].name});
									li.append(a);
									$(".orgList").append(li);
							        }
									//绑定人员点击事件
						    	    bindClick_tongxunlu.clickPerson();
							}
					   }
									//绑定点击通讯录的“确认”按键的事件
									bindClick_tongxunlu.clickConfirm_tonxunlu(fromWhere);
				}else{
					alert(result.error);
				}
			}
		});	
	},
		//获取搜索内容的ajax请求
		ajaxOfGetSearchPersonInfo : function(searchContent){
			$.ajax({
				type : "POST",
				dataType : "JSON",
				async:true,
				url : basePath + '/tongxunlu/searchPersonInfo.do',
				data :{"searchContent":searchContent},
				success : function(result) {
					if(true==result.success){
						var list_person = result.data;
						$(".parentOrg").remove();
					    $(".person").remove();
					    $(".org").remove();
					    $(".noResultTips").hide();
					    if(list_person[0]!="noResult"){
					    	for(var i=0;i<list_person.length;i++){
								var photourl = list_person[i].photourl==""?basePath+'/res/images/noPhotoUrl.jpg':list_person[i].photourl;
								var img = $('<img>',{'src':photourl,'style':'max-width: 40px;max-height: 40px;border-radius:40px'});
								var div_col2 = $('<div>',{'class':'col-xs-2'});
								div_col2.append(img);
								
								var span_name = $('<span>',{'style':'display: block;'}).text(list_person[i].name);
								var job = list_person[i].jobtitle;
								if(job.length>16){
									job=job.substr(0, 12)+"..."
								}
								var span_job = $('<span>',{'style':'font-size:12px;color:#969696;'}).text(job);
								var div_col8 = $('<div>',{'class':'col-xs-8'});
								div_col8.append(span_name);
								div_col8.append(span_job);
								
								var a = $('<a>',{'style':'height:60px'});
								a.append(div_col2);
								a.append(div_col8);
								
								var li = $('<li>',{'class':'row person','photourl':photourl,'openid':list_person[i].oid,'name':list_person[i].name});
								li.append(a);
								$(".orgList").append(li);
						        }
					    	    //绑定选择人员的点击事件
					    	    bindClick_tongxunlu.clickPerson();
					    	    	
					    }else{
					    	functions_tongxunlu.ajaxOfLoadOrg(undefined,undefined,undefined);
					    	/*<div class="noResultTips" style="text-align: center;color:#9d9d9d;padding:10px;display:none">很可惜哦，没有找到您想要的结果</div>
					        var div_noResTips = $('<div>',{'class':'noResultTips','style':'text-align: center;color:#9d9d9d;padding:10px'}).text("很可惜哦，没有找到您想要的结果");*/
					        $(".noResultTips").show();
					    }
					    
						}else{
							alert(result.error);
						}
					}
				});
		},
		//监控搜索框输入的变化
		change:function(serachContent){
		  var content = serachContent.replace(/\s/ig,"");
		  if(content.length==0){
			  functions_tongxunlu.ajaxOfLoadOrg(undefined,undefined,undefined);
		  }else{
			  functions_tongxunlu.ajaxOfGetSearchPersonInfo(content);
		  }
		},
}

	var bindClick_tongxunlu = {

	//点击每个部门找子部门
	clickEveryOrg : function(){
		$(".org").unbind();
		$(".org").bind('click',function(){
			var id = $(this).attr("id");
			var clickedOrg = $(this);
			functions_tongxunlu.ajaxOfLoadOrg(id,undefined,clickedOrg);
		});	
	},
	//点击父部门找同级部门
	clickParentOrg : function(){
		$(".parentOrg").unbind();
		$(".parentOrg").bind('click',function(){
			var parentid = $(this).attr("parentid");
			functions_tongxunlu.ajaxOfLoadOrg(undefined,parentid,undefined);
		});	
	},
	//点击每个人员
	clickPerson : function(){
		$(".person").unbind();
		$(".person").bind('click',function(){
			var fromWhereOfactTongxunlu =  $("#fromWhereOfActTongxunlu").val();
			
			var openid = $(this).attr("openid");
			var photourl = $(this).attr("photourl");
			var name  = $(this).attr("name");
			//如果是从创建团队激活通讯录的，则可以选择多人
			if(fromWhereOfactTongxunlu=="createTeam"){
				//查看是否已经选择
				var user =$('.checkedPerson[openid='+openid+']'); 
				if(user.length==0){ 
					var img = $('<img>',{'src':photourl,'style':'max-width: 45px;max-height: 45px;border-radius:40px'});
					var span_closeMark = $('<span>',{'class':'glyphicon glyphicon-remove-circle closeMark','name':'closeMark','style':'font-size:15px;color:#26B3FB;position:absolute;'});
					var span_name = $('<span>',{'class':'name'}).text(name);
					var div_name = $('<div>',{'style':'text-align:center'});
					div_name.append(span_name);
				    var div_col2 = $('<div>',{'class':'col-xs-3 checkedPerson','style':'padding-bottom:15px;text-align:center','openid':openid})
				    div_col2.append(img);
				    div_col2.append(span_closeMark);
				    div_col2.append(div_name);
				    $(".checkedTeamerList").append(div_col2);
				}
				//如果是从选择项目负责人激活通讯录的人，只能选择一个人
			}else if(fromWhereOfactTongxunlu=="projectLeader"){
				    //查看是否已经选择
					var user =$('.checkedPerson[openid='+openid+']'); 
					if(user.length==0){ 
						$(".checkedPerson").remove();
						var img = $('<img>',{'src':photourl,'style':'max-width: 45px;max-height: 45px;border-radius:40px'});
						var span_closeMark = $('<span>',{'class':'glyphicon glyphicon-remove-circle closeMark','name':'closeMark','style':'font-size:15px;color:#26B3FB;position:absolute;'});
						var span_name = $('<span>',{'class':'name'}).text(name);
						var div_name = $('<div>',{'style':'text-align:center'});
						div_name.append(span_name);
					    var div_col2 = $('<div>',{'class':'col-xs-3 checkedPerson','style':'padding-bottom:15px;text-align:center','openid':openid})
					    div_col2.append(img);
					    div_col2.append(span_closeMark);
					    div_col2.append(div_name);
					    $(".checkedTeamerList").append(div_col2);
					}
				
			}
			//绑定删除已选人员的点击事件
    	    bindClick_tongxunlu.clickCheckedPerson();
		});
	},
	//点击每个已选择的人员，删除之
	clickCheckedPerson : function(){
		$(".checkedPerson").unbind();
			$(".checkedPerson").bind('click',function(){
				$(this).remove();
			});
	},
	//点击通讯录的“确认”按键
	clickConfirm_tonxunlu : function(){
		$(".confirm_tongxunlu").unbind();
		$(".confirm_tongxunlu").bind('click',function(){
			var fromWhereOfactTongxunlu =  $("#fromWhereOfActTongxunlu").val();
			//如果这个通讯录的激发来自创建团队的“+”号
			if(fromWhereOfactTongxunlu=="createTeam"){
				if($("#resource").val()=="fenpai"){
					$("#anniu").hide();
					$("#addperson").show();
					$(".checkedPerson").each(function(){
						var openid = $(this).attr("openid");
						var photourl = $(this).find("img").attr("src");
						var name  = $(this).find("span[class='name']").text();
						//查看是否已经选择
						var user =$('.checkedTeamer[openid='+openid+']'); 
						if(user.length==0){ 
							var img = $('<img>',{'src':photourl,'style':'max-width: 35px;max-height: 35px;border-radius:30px'});
							var span_closeMark = $('<span>',{'class':'glyphicon glyphicon-remove-circle closeMark','name':'closeMark','style':'font-size:15px;color:#26B3FB;position:absolute'});
							var span_name = $('<span>').text(name);
							var div_name = $('<div>',{'style':'text-align:center'});
							div_name.append(span_name);
						    var div_col1 = $('<div>',{'class':'col-xs-1 checkedTeamer newTeamer','style':'padding-bottom:15px;text-align:center','openid':openid,'tname':name})
						    div_col1.append(img);
						    div_col1.append(span_closeMark);
						    div_col1.append(div_name);
							$("#addperson").before(div_col1);
					     }
						
					});
					//点击删除人员按钮
					$(".closeMark").click(function(){
						$(this).parent().remove()
					})
				}else if($("#resource").val()=="fuze"){
					$("#addpersonfuze").show();
					$(".checkedPerson").each(function(){
						var openid = $(this).attr("openid");
						var photourl = $(this).find("img").attr("src");
						var name  = $(this).find("span[class='name']").text();
						//查看是否已经选择
						var user =$('.checkedTeamerfuze[openid='+openid+']'); 
						if(user.length==0){ 
							var img = $('<img>',{'src':photourl,'style':'max-width: 35px;max-height: 35px;border-radius:30px'});
							var span_closeMark = $('<span>',{'class':'glyphicon glyphicon-remove-circle closeMark','name':'closeMark','style':'font-size:15px;color:#26B3FB;position:absolute'});
							var span_name = $('<span>').text(name);
							var div_name = $('<div>',{'style':'text-align:center'});
							div_name.append(span_name);
						    var div_col1 = $('<div>',{'class':'col-xs-1 checkedTeamerfuze newTeamerfuze','style':'padding-bottom:15px;text-align:center','openid':openid,'tname':name})
						    div_col1.append(img);
						    div_col1.append(span_closeMark);
						    div_col1.append(div_name);
							$("#addpersonfuze").before(div_col1);
					     }
						
					});
					//点击删除人员按钮
					$(".closeMark").click(function(){
						$(this).parent().remove()
					})
				
				}
			}else if(fromWhereOfactTongxunlu=="projectLeader"){
				        var name = $(".checkedPerson").find("span[class='name']").text();
				        var openid = $(".checkedPerson").attr("openid");
				        $(".projectLeaderText").text(name);
				        $(".projectLeaderText").attr("name",name);
				        $(".projectLeaderText").attr("openid",openid);
			}
			$(".checkedPerson").remove();			
			
		});
	},
			
}